let app = getApp();
let API_URL = app.globalData.API_URL;
let iconUrl = 'https://atush.vip/static/icons/';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        showAuthDialog: false,
        currentItemIndex: null,
        favorotesList: [],
        destroyId: '',
        iconUrl: iconUrl
    },
    
    // 往详细页面
    onClickLikeDetailed(item) {
        let type = item.currentTarget.dataset.item.type
        let itemId = item.currentTarget.dataset.item.target_id
        switch (type) {
            case 23:
                wx.navigateTo({
                    url: '/pages/home/gold/goldProduct/goldProduct?shop_id=' + itemId
                });
                break;
            case 3:
                wx.navigateTo({
                    url: '/pages/home/house/houseDetaild/houseDetaild?itemId=' + itemId
                });
                break;
            case 4:
                wx.navigateTo({
                    url: '/pages/home/jobs/jobsDetailed/jobsDetailed?itemId=' + itemId
                });
                break;
            case 5:
                wx.navigateTo({
                    url: '/pages/home/car/carDetailed/carDetailed?itemId=' + itemId
                });
                break;
            case 6:
                wx.navigateTo({
                    url: '/pages/home/secondHand/secondDetailed/secondDetailed?itemId=' + itemId
                });
                break;
            case 7:
                wx.navigateTo({
                    url: '/pages/home/pickedUp/pikedUpDetailed/pikedUpDetailed?itemId=' + itemId
                });
                break;
            case 8:
                wx.navigateTo({
                    url: '/pages/home/throw/throwDetaild/throwDetaild?itemId=' + itemId
                });
                break;
            case 9:
                wx.navigateTo({
                    url: '/pages/home/tutor/tutorDetailed/tutorDetailed?itemId=' + itemId
                });
                break;
            case 10:
                wx.navigateTo({
                    url: '/pages/home/maintenance/maintenanceDetailed/maintenanceDetailed?itemId=' + itemId
                });
                break;
            case 13:
                wx.navigateTo({
                    url: '/pages/home/shop/shopDetailed/shopDetailed?itemId=' + itemId
                });
                break;
            case 14:
                wx.navigateTo({
                    url: '/pages/home/buyAndSell/buyAndSellDetaile/buyAndSellDetaile?itemId=' + itemId
                });
                break;
            case 15:
                console.log('信息圈详细页面');
                break;
            case 16:
                wx.navigateTo({
                    url: '/pages/home/price/priceDetailed/priceDetailed?itemId=' + itemId
                });
                break;
            default:
                break;
        }
    },
    
    /**
     * 删除发布内容
     */
    // 获取内容
    getPost() {
        let user_id = wx.getStorageSync('uid')
        let that = this
        
        wx.request({
            url: `${API_URL}/getFavorites`,
            data: {user_id},
            header: app.header(),
            method: 'post',
            success: function (res) {
                that.setData({ favorotesList: res.data })
            }
        })
    },

    // 获取 Id
    getIndexById(id, arr) {
        this.setData({ destroyId: id })
        for ( let i in arr ) {
            if ( id == arr[i].id ) {
                return i
            }
        }
        return -1;
    },

    // 确定删除对
    dialogOnConfirm() {
        let token = null;
        let destroyId = this.data.destroyId;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                token = res.data.token
                wx.request({
                    url: `${API_URL}/favoritesDestroy`,
                    header: {
                        token: res.data.token
                    },
                    data: { postId: destroyId },
                    method: 'post',
                    success: function (res) {
                        console.log('res: ', res);
                    }
                })
            }
        })

        if (this.data.currentItemIndex) {
            let index = this.data.currentItemIndex
            let arr = this.data.favorotesList
            arr.splice(index, 1)
            this.setData({ favorotesList: arr })
        }
        this.setData({ showAuthDialog: false })
    },

    // 取消删除
    dialogOnCancel() {
        this.setData({ showAuthDialog: false })
    },
    
    // 弹出删除对话框
    showDialogtTap(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            showAuthDialog: true,
            currentItemIndex: this.getIndexById(item.id, this.data.favorotesList)
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "faroitesInfo", navigation: true })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})